// common/components/uniFilter/uniFilter.js  
import {
  handleCheckBoxChecked,
  SELECT_TYPE,
  selectedTypeNumComputed,
  handleSelectedPopup,
  mapToArr
} from '../../../utils/util.js';
const computedBehavior = require('miniprogram-computed').behavior

Component({
  behaviors: [computedBehavior, ],

  properties: {
    // 展示列表数据
    viewListData: {
      type: Array,
      value: []
    },
    // 备份列表数据
    copyViewListData: {
      type: Array,
      value: []
    },
    // 初始数据
    sourceData: {
      type: Array,
      value: []
    },
    // 是否多选
    multiple: {
      type: Boolean,
      value: true,
    },
    echoValue: {
      type: Array,
      value: []
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    visibleSelectPopup: false,
    // 已选类型数量
    selectedTypeNum: SELECT_TYPE,
    // 已选弹窗列表数据
    selectedPopupList: {},
    // // 展示列表数据 
    // sourceData: [],
    // // 备份列表数据 
    // copyViewListData: [],
    // // 初始数据 
    // viewListData: [],
    // 路径导航数据
    pathData: [],
    // 单选情况下展示当前点击数据路径
    currentValuePath: '',
    // 搜索关键词
    keywords: '',
    // 选中值的名称
    valueName: '',
    // 选中值的前缀
    valueKey: '',
    // 已选择的值
    checkedValue: [],
    //小组Id
    teamId: ''
  },
  watch: {
    'echoValue': function (value) {
      if (value) {
        this.setData({
          checkedValue: value,
          selectedTypeNum: selectedTypeNumComputed(value, this.data.sourceData),
          selectedPopupList: handleSelectedPopup(this.data.sourceData, value),
        })
      }
    }
  },
  pageLifetimes: {
    show() {
      console.log('show', this.data.echoValue);


    }
  },
  lifetimes: {
    attached() {
      // console.log(this.data.dataList, 'this.data.dataList');
      // // const mockList = deptToConvert(MockDept)
      // const treeList = convertToTree(this.data.dataList, {
      //   id: 'deptId',
      //   parentId: 'parentId',
      //   name: 'deptName'
      // })
      // console.log(treeList, 'mockList');
      // this.setData({
      //   sourceData: this.data.dataList,
      //   copyViewListData: treeList,
      //   viewListData: treeList
      // })
    }
  },
  methods: {
    // 展开弹窗
    onExpand() {
      this.setData({
        visibleSelectPopup: true
      })
    },


    onChangeCheckBox(event) {
      const {
        currentTarget,
        detail
      } = event
      const {
        dataset
      } = currentTarget
      // 多选
      if (this.data.multiple) {
        const val = handleCheckBoxChecked(dataset.value, detail.checked, this.data.checkedValue)
        this.setData({
          checkedValue: val,
          selectedTypeNum: selectedTypeNumComputed(val, this.data.sourceData),
          selectedPopupList: handleSelectedPopup(this.data.sourceData, val),

        })
      } else {
        const {
          checked
        } = detail
        // 单选
        this.setData({
          checkedValue: checked ? [dataset.value] : '',
          valueName: checked ? dataset.name : '',
          valueKey: checked ? dataset.key : '',
          currentValuePath: checked ? dataset.path : "",
          teamId: checked ? dataset.teamid : ""
        })
      }
    },
    onNextIcon({
      currentTarget
    }) {
      const {
        dataset
      } = currentTarget

      const child = dataset.children
      const currentName = dataset.currentname
      const currentValue = dataset.currentvalue
      const {
        pathData
      } = this.data
      pathData.push({
        nodeName: currentName,
        value: currentValue
      })

      this.setData({
        viewListData: child,
        pathData: pathData,
        isAllSelected: false
      })
    },
    // 路径导航  全部
    onAll() {
      this.setData({
        viewListData: this.data.copyViewListData,
        pathData: []
      })
    },
    // 路径导航, 某个
    onPathItem({
      detail
    }) {
      const {
        index,
        value
      } = detail
      const pathData = this.data.pathData
      pathData.splice(index + 1)
      this.setData({
        viewListData: wx.$_.filter(this.data.copyViewListData, (item) => item._value === value)[0].children,
        pathData: pathData
      })
    },
    // 搜索框
    onChangeKeywords({
      detail
    }) {
      const keyword = detail.value
      if (keyword) {
        const filters = wx.$_.filter(this.data.sourceData, (item) => {
          return item.deptName.indexOf(keyword) > -1
        })
        this.setData({
          viewListData: filters
        })
      } else {
        this.setData({
          viewListData: this.data.copyViewListData,
        })
      }

    },
    // 移除
    onRemove({
      detail
    }) {
      const {
        value
      } = detail
      const arr = mapToArr(value)
      const values = wx.$_.map(arr, 'value')

      this.setData({
        selectedPopupList: value,
        checkedValue: values,
        selectedTypeNum: selectedTypeNumComputed(values, this.data.sourceData)
      })
    },
    // 全选
    onAllSelect() {
      let {
        checkedValue
      } = this.data
      const values = wx.$_.map(this.data.viewListData, '_value')
      const newValues = checkedValue.concat(values)
      this.setData({
        checkedValue: newValues,
        selectedTypeNum: selectedTypeNumComputed(newValues, this.data.sourceData),
        selectedPopupList: handleSelectedPopup(this.data.sourceData, newValues),
      })
    },
    // 确认
    onConfirm() {
      let val = null

      if (this.data.multiple) {
        val = mapToArr(this.data.selectedPopupList)
      } else {
        val = [{
          name: this.data.valueName,
          key: this.data.valueKey,
          value: this.data.checkedValue[0],
          teamId: this.data.teamId
        }]
      }

      this.triggerEvent('onConfirm', {
        value: val,
      })

    },
    // 取消
    onCancel() {
      this.triggerEvent('onCancel')
    },

  }

})